package com.linghang.wusthelper.spider.library;

import com.linghang.wusthelper.dao.primary.BookRentHistoryDao;
import com.linghang.wusthelper.entity.primary.BookRentHistory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * @author origin
 * 图书借阅历史爬虫
 */
public class LibRentHistorySpider implements PageProcessor {

    private String stuNum;

    private BookRentHistoryDao bookRentHistoryDao;

    private String lock;

    private Logger log = LoggerFactory.getLogger(getClass());

    private Site site = Site.me().setRetryTimes(3).setTimeOut(3000)
            .setDomain("www.lib.wust.edu.cn")
            .addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
            .addHeader("Accept-Encoding","gzip,deflate,sdch")
            .addHeader("Accept-Language", "zh-CN,zh;q=0.8")
            .addHeader("Connection", "keep-alive")
            .addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36");

    public LibRentHistorySpider(String PHPSESSID,String stuNum,BookRentHistoryDao bookRentHistoryDao,String lock){
        site.addCookie("PHPSESSID",PHPSESSID);
        this.stuNum = stuNum;
        this.bookRentHistoryDao = bookRentHistoryDao;
        this.lock = lock;
    }

    @Override
    public void process(Page page) {
//        System.out.println(page.getHtml().get());
        int i = 2;
        String path = "//*[@id=\"mylib_content\"]/table/tbody/tr[2]";
        bookRentHistoryDao.delete(stuNum);
        while (page.getHtml().xpath(path).get() != null){
            String bookCode = page.getHtml().xpath(path+"/td[2]/text()").get();
            String rentTime = page.getHtml().xpath(path+"/td[5]/text()").get();
            String bookName = page.getHtml().xpath(path+"/td[3]/a/text()").get();
            String url = page.getHtml().xpath(path+"/td[3]/a/@href").get();
            String bookAuthor = page.getHtml().xpath(path+"/td[4]/text()").get();
            String returnTime = page.getHtml().xpath(path+"/td[6]/text()").get();
            String bookPlace = page.getHtml().xpath(path+"/td[7]/text()").get();
            BookRentHistory bookRentHistory = new BookRentHistory();
            bookRentHistory.setStuNum(stuNum);
            bookRentHistory.setBookCode(bookCode);
            bookRentHistory.setBookName(bookName);
            bookRentHistory.setBookUrl(url);
            bookRentHistory.setBookAuthor(bookAuthor);
            bookRentHistory.setRentTime(rentTime);
            bookRentHistory.setReturnTime(returnTime);
            bookRentHistory.setBookPlace(bookPlace);

            bookRentHistoryDao.insert(bookRentHistory);

            i++;
            path = "//*[@id=\"mylib_content\"]/table/tbody/tr["+i+"]";
        }
        synchronized (lock){
            lock.notify();
        }
    }

    @Override
    public Site getSite() {
        return site;
    }
}
